<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="row" style="margin:0;padding:0;"> 
		<div class="col-xs-2" style="margin:0px auto;padding:0px;">
					 <div class="widget-box"  style="margin:0 auto;">
												<div class="widget-header widget-header-flat">
													<b>集团学校</b>
												</div> 
												<div class="widget-body"> 
													<div style="padding:4px;">
													 	<button id="id-btn-dialogk" class="btn btn-sm btn-primary">增加</button>
													 	<button id="id-btn-dialogc" class="btn btn-sm btn-primary">编辑</button>
													 	<button id="id-btn-dialogd" class="btn btn-sm btn-danger">删除</button>
													 </div>
												 	<ul id="treeDemo" class="ztree"></ul>
												</div>
					 </div>
			</div>
			<div class="col-xs-10"  id="class_mainContent" style="margin:0px auto;padding:0px;padding-left:4px;">
				
				
			</div>  
</div> 
<script>
(function(){
	var setting = {	
			view: {
				dblClickExpand: false,
				showLine: true,
				selectedMulti: false
			},callback: {
				onClick: zTreeOnClick
			},
			data: {
			simpleData: {
				enable:true,
				idKey: "cid",
				pIdKey: "cpid",
				rootPId: "0"
			}
		}};
		//点击事件
		function zTreeOnClick(event, treeId, treeNode) { 	
			if(treeNode.type==3 || treeNode.type==4){
				$("#class_mainContent").load("${pageContext.request.contextPath}/class_teacher_student.jsp?r="+Math.random(),{},function(){
					loadTeacher(null,{table_id:treeNode.table_id});
					loadUser(null,{table_id:treeNode.table_id});
				}) 
			}else if(treeNode.type==2){
				$("#class_mainContent").load("${pageContext.request.contextPath}/class_learnTime.jsp?r="+Math.random(),{table_id:treeNode.table_id},function(){
					
				});
			}else{
				$("#class_mainContent").html("");
			}
		};
	   function loadTree(){
		 $.ajax({
			   type: "POST",
			   url: "${pageContext.request.contextPath}/edu/sysClassAction.do?method=getAllClass&r="+Math.random(),				
			   dataType:"json",
			   success: function(msg){  
				   $.fn.zTree.init($("#treeDemo"), setting, msg[0].list);
			   }
			}); 
	 }
	function addClass(){
		//增加班
		$("#id-btn-dialogk").click(function(){
				 
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
				var nodes = treeObj.getSelectedNodes();
			  
				if(nodes!=null && nodes !="")
				{ 
					if(nodes[0].type==4){
						alert("对不起!系统设定班级下不在进行更细的划分！");
						return;
					} 
				}
			
			 	$("#user_include").html("");
			 	$("#user_include").load("${pageContext.request.contextPath}/classAdd.jsp?r="+Math.random(),{},function(){
			 		 
			 		var dialog = $("#dialog-message").removeClass("hide").dialog({
						modal: true,
						title: "增加集团",
						width:800,
						title_html: false,
						close:function(){
							$(this).dialog('destroy'); 
							$("#user_include").html("");
							//loadTree();
						},
						buttons: [  
							{
								text: "提交",
								"class" : "btn btn-primary btn-xs",
								click: function() {
									window.classAdd.addClass(this);
								} 
							}
						]
					}); 
				}); 
		}); 
		//删除
		$("#id-btn-dialogd").click(function(){
			var r=confirm("删除节点，会删除节点下的所有数据，你确认要删除这个节点吗?");
			  if (r==true){
				    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
					var nodes = treeObj.getSelectedNodes();
					if(nodes!=null && nodes.length>0){
						$.ajax({
							   type: "POST",
							   async:false,
							   url: "${pageContext.request.contextPath}/edu/sysClassAction.do?method=deleteClass&r="+Math.random(),
							   data: {table_id:nodes[0].table_id,pName:nodes[0].pName},
							   dataType:"json",
							   success: function(msg){
								 
							      if(msg.code==1){
							    	  alert(msg.message);
							    	  loadTree();
							      }else{
							    	  alert(msg.message);
							      }
							   }
						});
					}else{
						alert("未选中节点");
					}
					
			   }
			  
		});
		//修改
		$("#id-btn-dialogc").click(function(){
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
				var nodes = treeObj.getSelectedNodes();
				if(nodes!=null && nodes.length>0){
					if(nodes[0].type==3){
						alert("学校年级层级由国家规定不能进行修改!");
						return;
					}
					$("#user_include").html("");
				 	$("#user_include").load("${pageContext.request.contextPath}/classUpdate.jsp?r="+Math.random(),nodes[0],function(){
				 		 
				 		var dialog = $("#dialog-message").removeClass("hide").dialog({
							modal: true,
							title: "增加集团",
							width:800,
							title_html: false,
							close:function(){
								$(this).dialog('destroy'); 
								$("#user_include").html("");
								//loadTree();
							},
							buttons: [  
								{
									text: "提交",
									"class" : "btn btn-primary btn-xs",
									click: function() {
										window.classUpdate.updateClass(this);
									} 
								}
							]
						}); 
					}); 
				}else{
					alert("未选中节点！");
				}
		});
	};
	//查询老师 
	function loadTeacher(event,data){
		if(typeof(data)=="undefined"){
			data={};
		}
		var url ="${pageContext.request.contextPath}/edu/sysUserAction.do?method=getClassTeacher";
		var cp =$("#teacher_pagination").find(".active");
		data.start=1;
		if(cp.length>0){
			var ccp=$(cp[0]).text();
			if(parseInt(ccp)){
				data.start=ccp;
			}
		}  
		$("#table_teacher").wgrid({
					table_title:"sys_teacher_table_title",
					tbodyId:"sys_teacher_table",
					pagination:"teacher_pagination",
					url:url,
					//checkbox:true,
					data:data,
					success:function(){
						changeTeacherImage();
					}
		});
	};
	//查询学生 
	function loadUser(event,data){
		if(typeof(data)=="undefined"){
			data={};
		}
		var url ="${pageContext.request.contextPath}/edu/sysUserAction.do?method=getClassStudent";
	 
		var cp =$("#user_pagination").find(".active");
		data.start=1;
		if(cp.length>0){
			var ccp=$(cp[0]).text();
			if(parseInt(ccp)){
				data.start=ccp;
			}
		}  
		$("#table_user").wgrid({
					table_title:"sys_user_table_title",
					tbodyId:"sys_user_table",
					pagination:"user_pagination",
					url:url,
					//checkbox:true,
					data:data,
					success:function(){
						//changeUserState();
						changeUserImage();
					}
		});
	};
	//改变头像 和性别，还有类型
	function changeUserImage(){ 
		 
		var allstate =$("div[name='sys_user_table_sex_tr_td']");
		for(var i=0;i<allstate.length;i++){
			var bc = $(allstate[i]);
			if(bc.text()==1){
				bc.html('<font color="green">男</font>');
			}else if(bc.text()==0){
				bc.html('<font color="red">女</font>');
			}
		} 
	}
	//改变头像 和性别，还有类型
	function changeTeacherImage(){ 
		 
		var allstate =$("div[name='sys_teacher_table_sex_tr_td']");
		for(var i=0;i<allstate.length;i++){
			var bc = $(allstate[i]);
			if(bc.text()==1){
				bc.html('<font color="green">男</font>');
			}else if(bc.text()==0){
				bc.html('<font color="red">女</font>');
			}
		} 
	}
	$(function(){
		addClass();
		loadTree();
	});
	window.class1={};
	window.class1.loadTree=loadTree;
})();
</script>